@@ -0,0 +1,82 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class BoxcarAgent < Agent |
|
| 3 |
+ |
|
| 4 |
+ cannot_be_scheduled! |
|
| 5 |
+ cannot_create_events! |
|
| 6 |
+ |
|
| 7 |
+ API_URL = 'https://new.boxcar.io/api/notifications' |
|
| 8 |
+ |
|
| 9 |
+ description <<-MD |
|
| 10 |
+ The Boxcar agent sends push notifications to iPhone. |
|
| 11 |
+ |
|
| 12 |
+ To be able to use the Boxcar end-user API, you need your `Access Token`. |
|
| 13 |
+ The access token is available on general "Settings" screen of Boxcar iOS |
|
| 14 |
+ app or from Boxcar Web Inbox settings page. |
|
| 15 |
+ |
|
| 16 |
+ Please provide your access token in the `user_credentials` option. If |
|
| 17 |
+ you'd like to use a credential, set the `user_credentials` option to `{%
|
|
| 18 |
+ credential CREDENTIAL_NAME %}`. |
|
| 19 |
+ |
|
| 20 |
+ Options: |
|
| 21 |
+ |
|
| 22 |
+ * `user_credentials` - Boxcar access token. |
|
| 23 |
+ * `title` - Title of the message. |
|
| 24 |
+ * `body` - Body of the message. |
|
| 25 |
+ * `source_name` - Name of the source of the message. Set to `Huginn` by default. |
|
| 26 |
+ * `icon_url` - URL to the icon. |
|
| 27 |
+ * `sound` - Sound to be played for the notification. Set to 'bird-1' by default. |
|
| 28 |
+ MD |
|
| 29 |
+ |
|
| 30 |
+ def default_options |
|
| 31 |
+ {
|
|
| 32 |
+ 'user_credentials' => '', |
|
| 33 |
+ 'title' => "{{title}}",
|
|
| 34 |
+ 'body' => "{{body}}",
|
|
| 35 |
+ 'source_name' => "Huginn", |
|
| 36 |
+ 'icon_url' => "", |
|
| 37 |
+ 'sound' => "bird-1" |
|
| 38 |
+ } |
|
| 39 |
+ end |
|
| 40 |
+ |
|
| 41 |
+ def working? |
|
| 42 |
+ received_event_without_error? |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ def strip(string) |
|
| 46 |
+ (string || '').strip |
|
| 47 |
+ end |
|
| 48 |
+ |
|
| 49 |
+ def validate_options |
|
| 50 |
+ errors.add(:base, "you need to specify a boxcar api key") if options['user_credentials'].blank? |
|
| 51 |
+ end |
|
| 52 |
+ |
|
| 53 |
+ def receive(incoming_events) |
|
| 54 |
+ incoming_events.each do |event| |
|
| 55 |
+ payload_interpolated = interpolated(event) |
|
| 56 |
+ user_credentials = payload_interpolated['user_credentials'] |
|
| 57 |
+ post_params = {
|
|
| 58 |
+ 'user_credentials' => user_credentials, |
|
| 59 |
+ 'notification' => {
|
|
| 60 |
+ 'title' => strip(payload_interpolated['title']), |
|
| 61 |
+ 'long_message' => strip(payload_interpolated['body']), |
|
| 62 |
+ 'source_name' => payload_interpolated['source_name'], |
|
| 63 |
+ 'sound' => payload_interpolated['sound'], |
|
| 64 |
+ 'icon_url' => payload_interpolated['icon_url'] |
|
| 65 |
+ } |
|
| 66 |
+ } |
|
| 67 |
+ send_notification(post_params) |
|
| 68 |
+ end |
|
| 69 |
+ end |
|
| 70 |
+ |
|
| 71 |
+ def send_notification(post_params) |
|
| 72 |
+ response = HTTParty.post(API_URL, :query => post_params) |
|
| 73 |
+ raise StandardError, response['error']['message'] if response['error'].present? |
|
| 74 |
+ if response['Response'].present? && response['Response'] == "Not authorized" |
|
| 75 |
+ raise StandardError, response['Response'] |
|
| 76 |
+ end |
|
| 77 |
+ if !response['id'].present? |
|
| 78 |
+ raise StandardError, "Invalid response from Boxcar: #{response}"
|
|
| 79 |
+ end |
|
| 80 |
+ end |
|
| 81 |
+ end |
|
| 82 |
+end |
@@ -0,0 +1,60 @@ |
||
| 1 |
+require 'rails_helper' |
|
| 2 |
+ |
|
| 3 |
+describe Agents::BoxcarAgent do |
|
| 4 |
+ before(:each) do |
|
| 5 |
+ @valid_params = {
|
|
| 6 |
+ 'user_credentials' => 'access_token', |
|
| 7 |
+ 'title' => 'Sample Title', |
|
| 8 |
+ 'body' => 'Sample Body' |
|
| 9 |
+ } |
|
| 10 |
+ @checker = Agents::BoxcarAgent.new(:name => "boxcartest", :options => @valid_params) |
|
| 11 |
+ @checker.user = users(:bob) |
|
| 12 |
+ @checker.save! |
|
| 13 |
+ |
|
| 14 |
+ @event = Event.new |
|
| 15 |
+ @event.agent = agents(:bob_weather_agent) |
|
| 16 |
+ @event.payload = { :body => 'Sample message' }
|
|
| 17 |
+ @event.save! |
|
| 18 |
+ end |
|
| 19 |
+ |
|
| 20 |
+ describe 'validating' do |
|
| 21 |
+ before do |
|
| 22 |
+ expect(@checker).to be_valid |
|
| 23 |
+ end |
|
| 24 |
+ |
|
| 25 |
+ it "should require access token" do |
|
| 26 |
+ @checker.options['user_credentials'] = nil |
|
| 27 |
+ expect(@checker).not_to be_valid |
|
| 28 |
+ end |
|
| 29 |
+ end |
|
| 30 |
+ |
|
| 31 |
+ describe '#working?' do |
|
| 32 |
+ it "should not be working until the first event was received" do |
|
| 33 |
+ expect(@checker).not_to be_working |
|
| 34 |
+ @checker.last_receive_at = Time.now |
|
| 35 |
+ expect(@checker).to be_working |
|
| 36 |
+ end |
|
| 37 |
+ end |
|
| 38 |
+ |
|
| 39 |
+ describe "#receive" do |
|
| 40 |
+ it "sends a message" do |
|
| 41 |
+ stub(HTTParty).post { {"id" => 1, "message" => "blah", "title" => "blah","source_name" => "Custom Notification"} }
|
|
| 42 |
+ @checker.receive([@event]) |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ it "should raise error when invalid response arrives" do |
|
| 46 |
+ stub(HTTParty).post { {"blah" => "blah"} }
|
|
| 47 |
+ expect{@checker.send_notification}.to raise_error
|
|
| 48 |
+ end |
|
| 49 |
+ |
|
| 50 |
+ it "should raise error when response says unauthorized" do |
|
| 51 |
+ stub(HTTParty).post '{"Response":"Not authorized"}'
|
|
| 52 |
+ expect{@checker.send_notification}.to raise_error
|
|
| 53 |
+ end |
|
| 54 |
+ |
|
| 55 |
+ it "should raise error when response has an error" do |
|
| 56 |
+ stub(HTTParty).post '{"error": {"message": "Sample error"}}'
|
|
| 57 |
+ expect{@checker.send_notification}.to raise_error
|
|
| 58 |
+ end |
|
| 59 |
+ end |
|
| 60 |
+end |